package com.monika.main.system.lock.impl;

import com.monika.main.system.lock.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:whh
 * @date: 2024-05-11 18:47
 * <p></p>
 */

@Component
public class JdbcRowLock implements Lock {

    private static final Logger LOG = LoggerFactory.getLogger(JdbcRowLock.class);

    private static final String LOCK_NAME = "JDBC_LOCK_NAME";

    private static final int LOCK = 1;

    private static final int UNLOCK = 0;

    private static final String SELECT_SQL = "select lock_name,lock_status from tb_lock where lock_name = ? for update";


    private static final String UPDATE_SQL = "update tb_lock set lock_status = ? where lock_name = ?";



    private JdbcTemplate jdbcTemplate;

    /**
     * 事务
     */
    private TransactionTemplate transactionTemplate;





    @Override
    public boolean lock() {
        Boolean lock = transactionTemplate.execute(action -> {
            Map<String, Object> result = jdbcTemplate.query(SELECT_SQL, rs -> {
                Map<String, Object> map = new HashMap<>();
                map.put("lock_name", rs.getString(1));
                map.put("lock_status", rs.getInt(2));
                return map;
            }, LOCK_NAME);

            int lockStatus = (int) result.get("lock_status");
            if (LOCK == lockStatus) {
                LOG.info("线程{}获取锁[{}]失败，当前锁已被占用", Thread.currentThread().getName(), LOCK_NAME);
                return false;
            }
            jdbcTemplate.update(UPDATE_SQL, ps -> {
                ps.setInt(1, LOCK);
                ps.setString(2, LOCK_NAME);
            });
            LOG.info("线程{}获取锁[{}]成功", Thread.currentThread().getName(), LOCK_NAME);
            return true;
        });
        return lock;
    }

    @Override
    public void unlock() {
        jdbcTemplate.update(UPDATE_SQL, ps -> {
            ps.setInt(1, UNLOCK);
            ps.setString(2, LOCK_NAME);
        });
        LOG.info("线程{}释放锁[{}]成功", Thread.currentThread().getName(), LOCK_NAME);
    }

    @Override
    public void interrupt() {

    }

    @Autowired
    public JdbcRowLock(JdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        this.transactionTemplate = transactionTemplate;
    }
}
